package cn.edu.abtu.service.impl;

import cn.edu.abtu.dao.AclPermissionDao;
import cn.edu.abtu.dao.AclRolePermissionDao;
import cn.edu.abtu.entity.AclPermission;
import cn.edu.abtu.entity.AclRolePermission;
import cn.edu.abtu.service.AclPermissionService;
import cn.edu.abtu.vo.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class AclPermissionServiceImpl implements AclPermissionService {

    @Resource
    private AclPermissionDao aclPermissionDao;

    @Resource
    private AclRolePermissionDao aclRolePermissionDao;

    @Override
    public ResponseData queryAllMenu() {
        // 1.查询菜单表所有数据
        QueryWrapper<AclPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","pid","name","type","permission_value","path","component","icon","status","is_deleted","gmt_create","gmt_modified");
        queryWrapper.orderByAsc("id");
        List<AclPermission> aclPermissionList = aclPermissionDao.selectList(queryWrapper);

        // 2.把查询所有菜单list集合按照要求进行封装
        List<AclPermission> resultList = buildPermission(aclPermissionList);

        ArrayList<Object> objects = new ArrayList<>();

        return ResponseData.success("成功",resultList);
    }

    @Override
    public ResponseData delete(String id) {
        // 1.创建一个list集合,用于封装所有删除菜单的id值
        List<String> idList = new ArrayList<>();
        // 2.向list中设置需要删除的菜单id
        this.selectPermissionChildById(id,idList);
        idList.add(id);
        int i = aclPermissionDao.deleteBatchIds(idList);
        return ResponseData.success("删除成功!",null);
    }

    @Override
    public ResponseData doAssign(String roleId, String[] permissionIds) {
        // 1.创建list集合,用于封装添加数据
        List<AclRolePermission> rolePermissionList = new ArrayList<>();
        // 2. 遍历所有菜单数组
        for (String permissionId : permissionIds) {
            AclRolePermission rolePermission = new AclRolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermission.setIsDeleted(0);
            rolePermission.setGmtCreate(new Date());
            rolePermission.setGmtModified(new Date());
            rolePermissionList.add(rolePermission);
        }
        // 3.添加到角色关系表中
        int insertBatch = aclRolePermissionDao.insertBatch(rolePermissionList);
        return ResponseData.success("添加角色关系成功",null);
    }

    public static List<AclPermission> buildPermission(List<AclPermission> aclPermissionList){
        //创建list集合,用于最终数据的封装
        List<AclPermission> finalList = new ArrayList<>();
        for (AclPermission aclPermission : aclPermissionList) {
            if (aclPermission.getPid().equals("0")){
                //设置顶层菜单的level是1
                aclPermission.setLevel(1);
                //根据顶层菜单,向里面进行查询子菜单,分装到finalList里边
                finalList.add(selectChildren(aclPermission, aclPermissionList));
            }
        }
        return finalList;
    }

    public static AclPermission selectChildren(AclPermission aclPermission, List<AclPermission> aclPermissionList){
        // 1.向一层菜单中放二层菜单,二层菜单里边方三层菜单,把对象初始化
        aclPermission.setChildren(new ArrayList<AclPermission>());
        // 2.遍历所有菜单的list集合,进行判断比较,比较id值和pid是否相同
        for (AclPermission item : aclPermissionList) {
            // 判断一级节点的id和二级节点的pid是否相同
            if (aclPermission.getId().equals(item.getPid())){
                //把父菜单的level值加一
                Integer level = aclPermission.getLevel() + 1;
                item.setLevel(level);
                //如果Children为空,进行初始化操作
                if (aclPermission.getChildren() == null){
                    aclPermission.setChildren(new ArrayList<AclPermission>());
                }
                //把查询到的二级菜单放到父菜单中
                aclPermission.getChildren().add(selectChildren(item, aclPermissionList));
            }
        }
        return aclPermission;
    }

    //根据当前id,查询菜单里边子菜单id,封装到list集合
    public void selectPermissionChildById(String id,List<String> idList){
        //查询菜单中的子菜单id
        QueryWrapper<AclPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",id);
        queryWrapper.select("id");
        List<AclPermission> childIdList = aclPermissionDao.selectList(queryWrapper);
        childIdList.stream().forEach(item->{
            //封装到idList里边
            idList.add(item.getId());
            //递归查询
            this.selectPermissionChildById(item.getId(),idList);
        });
    }
}
